<template>
    <div>
        <el-dialog
            :title="info.isAdd ? '菜单添加' : '菜单编辑'"
            :visible.sync="info.isshow"
            @closed="closed"
        >
            <div>form:{{ form }}</div>
            <el-form :model="form">
                <el-form-item label="所属角色" :label-width="formLabelWidth">
                    <el-select v-model="form.roleid" placeholder="请选择职务" @change="changePid">
                        <!-- 遍历list -->
                        <el-option
                            v-for="item in roleList"
                            :key="item.id"
                            :label="item.rolename"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="用户名" :label-width="formLabelWidth">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="form.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" v-if="info.isAdd" @click="add">添加</el-button>
                <el-button type="primary" v-else @click="edit">修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { requseradd, requserinfo, requseredit, reqrolelist } from "../../../http/api"
import { IndexRoutes } from "../../../router"
import { errorAlert, successAlert } from "../../../utils/alert"

export default {
    props: ["info", "list"],
    data() {
        return {
            //角色列表
            roleList: [],
            form: {
                roleid: "",
                username: "",
                password: "",
                status: 1,
            },
            formLabelWidth: "120px",
            IndexRoutes,
        }
    },
    computed: {
        ...mapGetters({
            user: "user"
        }),
    },
    mounted() {
        //一进来就要请角色
        reqrolelist().then(res => {
            if (res.data.code == 200) {
                this.roleList = res.data.list
            }
        })
    },
    methods: {
        ...mapActions({
            changeUser: "changeUser"
        }),
        //清空表单的数据
        empty() {
            this.form = {
                roleid: "",
                username: "",
                password: "",
                status: 1,
            }
        },

        checkDate() {
            return new Promise((resolve, rejeck) => {
                //做验证
                if (this.form.roleid === "") {
                    errorAlert("所属角色不能为空");
                    return;
                }
                if (this.form.username === "") {
                    errorAlert("用户名不能为空")
                    return;
                }
                if (this.form.password == "") {
                    errorAlert("密码不能为空")
                    return;
                }
                resolve();
            })
        },

        //点了添加
        add() {
            this.checkDate().then(() => {
                requseradd(this.form).then(res => {
                    if (res.data.code == 200) {
                        //弹窗成功
                        successAlert(res.data.msg);
                        //调用empty清空表单内容
                        this.empty();
                        //关闭弹框
                        this.info.isshow = false;
                        //刷新列表
                        this.$emit("init")
                    }
                })
            })
        },
        //点了取消
        cancel() {
            this.info.isshow = false
        },
        //修改了上级菜单
        changePid() {
            if (this.form.pid == 0) {
                this.form.type = 1
            } else {
                this.form.type = 2
            }
        },
        //编辑渲染页面
        getOne(id) {
            requserinfo({ uid: id }).then(res => {
                if (res.data.code == 200) {
                    this.form = res.data.list
                    this.form.password = "";
                }
            })
        },
        checkDate2() {
            return new Promise((resolve, rejeck) => {
                if (this.form.roleid === "") {
                    errorAlert("所属角色不能为空");
                    return;
                }
                if (this.form.username === "") {
                    errorAlert("用户名不能为空")
                    return;
                }
                resolve();
            })
        },
        //修改
        edit() {
            this.checkDate2().then(() => {
                requseredit(this.form).then(res => {
                    if (this.form.uid == this.user.uid) {
                        successAlert("修改成功")
                        this.empty();
                        //清空仓库数据
                        this.changeUser({})
                        this.$router.replace("/login")
                        return;
                    }
                    if (res.data.code == 200) {
                        //成功弹窗
                        successAlert(res.data.msg);
                        //清除数据表单
                        this.empty();
                        //关闭窗口
                        this.info.isshow = false
                        //刷新页面
                        this.$emit("init")
                    }
                })
            })
        },
        //弹框关闭
        closed() {
            if (this.info.isAdd == false) {
                this.empty()
            }
        }
    },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>